package com.bjqh.edu.UI.Activity.StudentPersonal;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.bjqh.edu.Base.AppConstants;
import com.bjqh.edu.Base.EasyActivity;
import com.bjqh.edu.Bean.MyPlan;
import com.bjqh.edu.R;
import com.bjqh.edu.UI.dialog.LoadDialog;
import com.bjqh.edu.adapter.PlanAdapter;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.cache.CacheMode;
import com.lzy.okgo.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class AcademicPlanningActivity extends ListActivity implements SwipeRefreshLayout.OnRefreshListener {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.rl_top_tilte)
    RelativeLayout rlTopTilte;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;
    @BindView(android.R.id.list)
    ListView list;
    @BindView(android.R.id.empty)
    LinearLayout empty;
    private PlanAdapter adapter;
    private List<MyPlan.DataBean> dataBeen;
    private LoadDialog dialog;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_academic_planning);
        ButterKnife.bind(this);
        list.setEmptyView(empty);
        swipeLayout.setColorSchemeResources(R.color.grean);
        swipeLayout.setOnRefreshListener(this);
        initViews();
        initData();
        initEvents();
    }
    protected void initViews() {
    }
    protected void initData() {
        tvTitle.setText("学业规划");
        dialog = new LoadDialog(AcademicPlanningActivity.this,"火速加载中");
        dialog.initDialog(AcademicPlanningActivity.this,"火速加载中");
        OkGo.post(AppConstants.BASE_URL + "/splan/datalist.shtml")
                .tag(this)
                .cacheKey("cacheKey")
                .cacheMode(CacheMode.DEFAULT)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        swipeLayout.setRefreshing(false);
                        LoadDialog.closeDialog();
                        Log.e("我的学业规划", s);
                        Gson gson = new Gson();
                        MyPlan myPlan = gson.fromJson(s, MyPlan.class);
                        if (myPlan.getMsg() != null) {
                            if (myPlan.getMsg().equals("timeout")) {
                                AppConstants.gotoLogin(AcademicPlanningActivity.this);
                            } else if (myPlan.getMsg().equals("remote login")) {
                                AppConstants.remologin(AcademicPlanningActivity.this);
                            }
                        } else {
                          /*  //测试数据
                            list =new ArrayList<String>();
                            list.add("1");
                            list.add("1");
                            list.add("1");*/
                            dataBeen = myPlan.getData();
                            //添加适配器
                            if (dataBeen.size() != 0) {
                                adapter = new PlanAdapter(AcademicPlanningActivity.this, dataBeen);
                                list.setAdapter(adapter);
                            }
                        }
                    }
                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        Toast.makeText(AcademicPlanningActivity.this, "报告小主，服务器被外星人偷走啦~~", Toast.LENGTH_SHORT).show();
                        LoadDialog.closeDialog();
                    }
                });
    }
    protected void initEvents() {
        ivBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    public void onRefresh() {
        initData();
    }
}
